<template>
  <div class="page-box">
    <!-- 搜索区域 -->
    <el-collapse-transition>
      <div v-show="showSearchBox" class="search-box">
        <!-- :inline="true" 表单在一行显示 -->
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="广告位名称">
            <el-input v-model.trim="queryParams.advposname" placeholder="请输入广告位名称" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-collapse-transition>
    <!-- 工具条区域 -->
    <div class="toolbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" @click="showDialog = true">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download">导出</el-button>
        </div>
        <div>
          <el-tooltip content="关闭搜索区" placement="top">
            <el-button icon="el-icon-arrow-up" circle @click="showSearchBox = !showSearchBox" />
          </el-tooltip>
          <el-tooltip content="刷新" placement="top">
            <el-button icon="el-icon-refresh" circle />
          </el-tooltip>
          <el-tooltip content="筛选" placement="top">
            <el-button icon="el-icon-menu" circle />
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table
        :data="list"
        style="width: 100%"
        border
      >
        <el-table-column type="index" label="#" width="50" />
        <el-table-column prop="advposname" label="广告位名称" />
        <el-table-column prop="advposdesc" label="广告位描述" />
        <el-table-column prop="advposnum" label="广告图数量" />
        <el-table-column prop="advpossize" label="广告位尺寸" />
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="primary" circle icon="el-icon-edit" @click="doEdit(row.id)" />
            <el-button type="danger" circle icon="el-icon-delete" @click="doDel(row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域 -->
    <div class="pagination-box">
      <el-row type="flex" justify="end">
        <el-pagination
          background
          :current-page="queryParams.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryParams.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- 新增和编辑弹出对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="showDialog" width="35%" @closed="resetForm">
      <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="广告位名称" prop="advposname">
          <el-input v-model="formData.advposname" style="width:90%" placeholder="请输入广告位名称" />
        </el-form-item>
        <el-form-item label="广告位描述" prop="advposdesc">
          <el-input v-model="formData.advposdesc" style="width:90%" type="textarea" placeholder="请输入广告位描述" :autosize="{ minRows: 2, maxRows: 4}" />
        </el-form-item>
        <el-form-item label="广告位尺寸" prop="advpossize">
          <el-input v-model="formData.advpossize" style="width:90%" placeholder="请输入广告位尺寸" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addPos, delPos, editPos, getPosDetail, getPosList } from '@/api/adv/pos'
export default {
  data() {
    return {
      // 控制是否显示搜索区
      showSearchBox: true,
      // 控制编辑和新增表单对话框显示
      showDialog: false,
      // 查询参数对象
      queryParams: {
        advposname: '',
        page: 1,
        limit: 10
      },
      // 表格数据
      list: [],
      // 数据总量 分页器用
      total: 0,
      formData: {
        advposname: '',
        advposdesc: '',
        advpossize: ''
      },
      rules: {
        advposname: [{ required: true, message: '请输入广告位名称', trigger: 'blur' }],
        advposdesc: [{ required: true, message: '请输入广告位描述', trigger: 'blur' }],
        advpossize: [{ required: true, message: '请输入广告位尺寸', trigger: 'blur' }]
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.formData.id ? '编辑广告位' : '新增广告位'
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 表格数据初始化
    async initData() {
      const res = await getPosList(this.queryParams)
      this.list = res.data
      this.total = res.count
    },
    // 搜索
    search() {
      this.queryParams.page = 1
      this.initData()
    },
    // 重置搜索
    reset() {
      this.queryParams = {
        advposdesc: '',
        advposname: '',
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // 切换每页显示数量
    handleSizeChange(value) {
      this.queryParams.limit = value
      this.queryParams.page = 1
      this.initData()
    },
    // 切换分页
    handleCurrentChange(value) {
      this.queryParams.page = value
      this.initData()
    },
    // 提交新增和编辑dialog表单
    submit() {
      this.$refs.form.validate(async(isOK) => {
        if (!isOK) return
        if (this.formData.id) {
          await editPos(this.formData)
          this.$message.success('编辑广告位成功！')
        } else {
          await addPos(this.formData)
          this.$message.success('新增广告位成功！')
        }
        // 刷新表格数据
        this.initData()
        // 关闭dialog自动重置表单
        this.showDialog = false
      })
    },
    async doEdit(id) {
      this.formData = await getPosDetail(id)
      this.showDialog = true
    },
    doDel(id) {
      this.$confirm('确认删除该广告位?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPos(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 处理删除最后一页最后一条数据
        if (this.list.length === 1 && this.queryParams.page > 1) {
          this.queryParams.page--
        }
        // 刷新表格
        this.initData()
      }).catch(() => {
        return false
      })
    },
    // dialog关闭后自动执行，无需手动调用
    resetForm() {
      // 重置表单数据
      this.formData = {
        catename: '',
        icon: '',
        sort_num: 10
      }
      // 重置表单校验结果
      this.$refs.form.clearValidate()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
